package work02.w5.control;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.swing.text.html.HTMLDocument.Iterator;

import work02.w5.control.persistence.Storage;
import work02.w5.control.server.MyAlarmServer;
import work02.w5.control.server.MyCalendarServeCenter;
import work02.w5.control.server.MyEventFactory;
import work02.w5.control.server.MyEventType;
import work02.w5.model.DateWithLunar;
import work02.w5.model.ScheduleModel;

/**
 * 用于指定日期是否有预定计划
 * 
 * @author Eric
 * @date 2012-10-4
 */
public class Schedule {
	private final String PATH = "Control_Schedule";
	private List<ScheduleModel> schedules;
	Storage store;
	MyAlarmServer alarm;
	MyEventFactory eventFactory;
	MyCalendar mycal;
	private Date today = new Date();
	private Map<ScheduleModel, Integer> scheduleAlarmMap = new HashMap<ScheduleModel, Integer>();

	public Schedule() {
		store = Storage.getInstance();
		alarm = MyAlarmServer.getInstance();
		eventFactory = MyEventFactory.getNewInstance();
		mycal = new MyCalendar();
		load();
		java.util.Iterator<ScheduleModel> its = schedules.iterator();
		while (its.hasNext()) {
			ScheduleModel it = its.next();
			if (it.getEndTime().getTime() <= today.getTime())
				addAlarm(it);
		}
	}

	private void load() {
		Object obj = null;
		try {
			obj = store.load(PATH);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (obj != null && obj instanceof List) {
			try {
				schedules = (List<ScheduleModel>) obj;
			} catch (Exception e) {
				schedules = new ArrayList<ScheduleModel>();
			}
		} else {
			schedules = new ArrayList<ScheduleModel>();
		}
	}

	private boolean save() {
		return store.save(PATH, schedules);
	}

	/**
	 * 改变一条预定
	 * 
	 * @param s
	 */
	public void changeSchedule(ScheduleModel news, ScheduleModel old) {
		removeShcedule(old);
		addSchedule(news);
	}

	/**
	 * 移除一条预定
	 * 
	 * @param s
	 */
	public void removeShcedule(ScheduleModel s) {
		schedules.remove(s);
		int id = scheduleAlarmMap.remove(s);
		alarm.removeAlarm(id);
		save();
	}

	/**
	 * 增加一条预定
	 * 
	 * @param s
	 */
	public void addSchedule(ScheduleModel s) {
		schedules.add(s);
		addAlarm(s);// 加入闹钟提醒
		/*
		 * 发送新事件消息
		 */
		MyCalendarServeCenter.getInstance().sendMyEvent(
				MyEventFactory.getNewInstance().produceEvent(this,
						MyEventType.addNewSchedule, "ScheduleModel", s));
		save();
	}

	/**
	 * 得到时间范围内的计划
	 * 
	 * @param from
	 *            开始时间
	 * @param end
	 *            结束时间
	 * @return 计划
	 */
	public ScheduleModel[] getSchedule(Date from, Date end) {
		List<ScheduleModel> ss = new ArrayList<ScheduleModel>();
		for (ScheduleModel s : schedules.toArray(new ScheduleModel[0])) {
			if (s.getStartTime().getTime() < from.getTime()
					&& s.getEndTime().getTime() >= end.getTime()||s.getStartTime().getTime() > from.getTime()&& s.getEndTime().getTime() <= end.getTime())
				ss.add(s);
		}
		return ss.toArray(new ScheduleModel[0]);
	}

	public ScheduleModel[] getSchedule(DateWithLunar aDay) {
		return getSchedule(mycal.getTheDayStart(aDay), mycal.getTheDayEnd(aDay));
	}

	/**
	 * 一段时间范围内是否有预定
	 * 
	 * @param from
	 * @param end
	 * @return
	 */
	public boolean hasSchedule(Date from, Date end) {
		for (ScheduleModel s : schedules.toArray(new ScheduleModel[0])) {
			if (s.getStartTime().getTime() < from.getTime()
					&& s.getEndTime().getTime() >= end.getTime()||s.getStartTime().getTime() > from.getTime()&& s.getEndTime().getTime() <= end.getTime())
				return true;
		}
		return false;
	}

	public void addAlarm(ScheduleModel it) {
		if (it.getEndTime().after(new Date())) {
			alarm.addAlarm(it.getStartTime(), eventFactory.produceEvent(this,
					MyEventType.ScheduleNoticeTime, "ScheduleModel", it));
		}
	}
}
